##########################
# Nadiya Kostyuk
# Creating plots
# R version: 4.1
# 11/12/25
##########################

rm(list=ls())

## Install & load packages (all at once)
list.of.packages <- c('dplyr', 'ggplot2', 
                      'lubridate')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]; if(length(new.packages)){install.packages(new.packages,dependencies=TRUE)}
lapply(list.of.packages, require, character.only = TRUE)


# setting directory: 
setwd("")



# loading data: 
load('data/data_final.RData')

load('data/data_final_not_for_sharing.RData')
data_final = data_final %>%
  mutate(outage_cause = CAUSE.x) %>%
  dplyr::select(DATE, ADM1_NAME, disruption_intentional_3indicators, disruption_intentional_3indicators_1dlag, 
                ua_control, ua_control_1dlag, contested_control, contested_control_1dlag, war_phases, war_phases_1dlag, war_phases_names,
                ukr_outages, 
                rus_mil_t, holiday_dummy, WEEKEND, log_rus_mil_t_1dlag, 
                territorial_control_80, territorial_control_80_1dlag, 
                territorial_control_70_1dlag, 
                rus_airstrike_t_1dlag, outage_cause,  control_ratio_moving_average)
save(data_final, file = 'data/data_final.RData')


##################################################
# Figure 2 (main document):
# Rus Outages
##################################################


############################
# Figure 2a:
# maneuver versus attrition
# on contested territories
##############################

outage_distribution <- data_final %>%
  mutate(`Territorial control` = ifelse(ua_control > 0.79| contested_control == 0, 'Ukrainian', 
                                        'Contested')) %>%
  # focusing only on contested territories:
  filter(`Territorial control` == 'Contested') %>%
  group_by(war_phases) %>%
  summarise(total_outages = mean(disruption_intentional_3indicators))%>%
  mutate(
    war_phases = case_when(
      war_phases == "Rus Maneuver Offensive" ~ 'Rus Offensive Maneuver', 
      war_phases == "Rus Maneuver Defensive" ~ 'Rus Defensive Maneuver', 
      war_phases == "Attrition" ~ 'Attrition'
    )
  ) %>%
  mutate(war_phases = factor(war_phases,
                             levels = c("Rus Offensive Maneuver", "Attrition", "Rus Defensive Maneuver")))
outage_distribution

# Create a bar chart
ggplot(outage_distribution, aes(x = war_phases, 
                                 y = total_outages)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  labs(
    x = "",
    y = "Proportion of Region-Days with \n Russia-attributed Outages"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  geom_text(aes(label = scales::percent(total_outages)), 
            position = position_dodge(width = 0.9), vjust = -0.2)
#ggsave("analysis/output/figures/rus_outage_distribution_contested_plot.pdf", width = 8, height = 6)


#############################
# Figure 2b: 
# punishment (Ukr-territories) versus maneuver/attrition (contested):
#############################

outage_distribution2 <- data_final %>%
  mutate(`Territorial control` = ifelse(ua_control > 0.79| contested_control == 0, 
                                        'Punishment on \n Ukrainian Territories', 
                                        'Maneuver & Attrition on \n Contested Territories')) %>%
  group_by(`Territorial control`) %>%
  summarise(total_outages = mean(disruption_intentional_3indicators, na.rm = TRUE)) %>% 
  ungroup()
outage_distribution2

ggplot(outage_distribution2, aes(x = `Territorial control`, 
                                 y = total_outages)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  labs(
    x = "",
    y = "Proportion of Region-Days with \n Russia-attributed Outages"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  geom_text(aes(label = scales::percent(total_outages)), 
            position = position_dodge(width = 0.9), vjust = -0.2)
#ggsave("analysis/output/figures/rus_outage_distribution_territories_plot.pdf", width = 8, height = 6)




##################################################
# Figure 7 (online appendix)
# Ukrainian outages
##################################################

# # merging with the Ukr outages data: 
# ukr_outages = read.csv('data/preprocessed/keep_in_on_ukr_outages.csv', header = T)
# ukr_outages = ukr_outages %>%
#   mutate(DATE = as.Date(as.character(DATE), format = "%Y%m%d")) %>%
#   dplyr::select(DATE, ADM1_NAME, CAUSE) %>%
#   mutate(reported_ukr_outage = 1)
# ukr_outages
# 
# data_final = data_final %>%
#   left_join(ukr_outages, by = c('DATE', 'ADM1_NAME')) %>% 
#   mutate(reported_ukr_outage = ifelse(is.na(reported_ukr_outage), 
#                                       0, reported_ukr_outage)) %>%
#   mutate(ukr_outages = ifelse(drop_b_bothID == 1 
#                               & reported_ukr_outage == 1, 1, 0))
# 
# 
# data_final %>%
#   filter(ukr_outages == 1) %>%
#   dplyr::select(DATE, ADM1_NAME, ukr_outages, drop_b_bothID, reported_ukr_outage, disruption_unintentional_3indicators)
# 

data_d_no_ukrcontrol_80 = data_final %>%
  filter(territorial_control_80_1dlag != 'Ukrainian control') %>%
  # Note: Note the flip - opposite strategies for each side
  mutate(
    war_phases_ukr = case_when(
      war_phases == 'Rus Maneuver Defensive' ~ 'Ukr Offensive Maneuver',
      war_phases == 'Rus Maneuver Offensive' ~ 'Ukr Defensive Maneuver',
      war_phases == 'Attrition' ~ 'Attrition'
    )
  )


##################################
# Figure 7a:
# Maneuver versus Attrition on 
# Contested Territories
##################################

outage_distribution3 <- data_d_no_ukrcontrol_80 %>%
  mutate(`Territorial control` = ifelse(territorial_control_80_1dlag == 'Russian occupation', 
                                        'Punishment on \n
                                        Russian-occupied Territories',
                                        'Maneuver & Attrition on \n Contested Territories')) %>%
  filter(`Territorial control` == 'Maneuver & Attrition on \n Contested Territories') %>%
  group_by(war_phases_ukr) %>%
  # will account for different amount of days in each phase:
  summarise(total_outages = mean(ukr_outages))%>%
  mutate(war_phases_ukr = factor(war_phases_ukr,
                                 levels = c("Ukr Offensive Maneuver", 
                                            "Attrition", "Ukr Defensive Maneuver")))
outage_distribution3

# Create a bar chart
# single plot and coloring + dodge
ggplot(outage_distribution3, aes(x = war_phases_ukr,
                                 y = total_outages)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  labs(
    x = "",
    y = "Proportion of Region-Days with \n Ukraine-attributed Outages"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label = scales::percent(total_outages)),
            position = position_dodge(width = 0.9), vjust = -0.2)
#ggsave("analysis/output/figures/ukr_outage_distribution_contested_plot.pdf", width = 8, height = 6)


###########################
# Figure 7b:
# Punishment  on Russian
# Occupied Territories:
###########################

outage_distribution4 <- data_d_no_ukrcontrol_80 %>%
  mutate(`Territorial control` = ifelse(territorial_control_80_1dlag == 'Russian occupation',
                                        'Punishment on \n Russian-occupied Territories', 
                                       'Maneuver & Attrition on \n Contested Territories')) %>%
  group_by(`Territorial control`) %>%
  summarise(total_outages = mean(ukr_outages))
outage_distribution4

# Create a bar chart
ggplot(outage_distribution4, aes(x = `Territorial control`,
                                 y = total_outages)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  labs(
    x = "",
    y = "Proportion of Region-Days with \n Ukraine-attributed Outages"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(aes(label = scales::percent(total_outages)),
            position = position_dodge(width = 0.9), vjust = -0.2)
#ggsave("analysis/output/figures/ukr_outage_distribution_territories_plot.pdf", width = 8, height = 6)




